//
//  SYNetworking.m
//  ffnetworking
//
//  Created by huchu on 2018/5/15.
//  Copyright © 2018年 sysjones. All rights reserved.
//

#import "SYNetworking.h"
#import "AFNetworking.h"
#import "RACSignal+SYAdd.h"
#import "ReactiveObjC.h"

@implementation SYNetworking {
    AFHTTPSessionManager*manager;
}


-(instancetype)init{
    self=[super init];
    
    return self;
}
-(void)createManger {
    manager= [AFHTTPSessionManager manager];
    
}

-(void)configRequest:(SYNetworkingRequestType)requestType{
    AFHTTPRequestSerializer <AFURLRequestSerialization> *requestSerializer=nil;
    switch (requestType) {
        case SYNetworkingRequestType_AFHTTPRequestSerializer:
            requestSerializer=[AFHTTPRequestSerializer serializer];
            break;
        case SYNetworkingRequestType_AFJSONRequestSerializer:
            requestSerializer=[AFJSONRequestSerializer serializer];
            break;
        case SYNetworkingRequestType_AFPropertyListRequestSerializer:
            requestSerializer=[AFPropertyListRequestSerializer serializer];
            break;
        default:
            break;
    }
    [requestSerializer setTimeoutInterval:60];
    [manager setRequestSerializer:requestSerializer];
    
}

-(void)configReponse:(SYNetworkingReponseType)responseType{
    AFHTTPResponseSerializer<AFURLResponseSerialization> *reponseSerializer=nil;
    switch (responseType) {
        case SYNetworkingResponseType_AFJSONResponseSerializer:
            reponseSerializer=[AFJSONResponseSerializer serializer];
            break;
        case SYNetworkingResponseType_AFHTTPResponseSerializer:
            reponseSerializer=[AFHTTPResponseSerializer serializer];
            break;
        case SYNetworkingResponseType_AFImageResponseSerializer:
            reponseSerializer = [AFImageResponseSerializer serializer];
            break;
        case SYNetworkingResponseType_AFCompoundResponseSerializer:
            reponseSerializer = [AFCompoundResponseSerializer serializer];
        case SYNetworkingResponseType_AFXMLParserResponseSerializer:
            reponseSerializer=[AFXMLParserResponseSerializer serializer];
            break;
        case SYNetworkingResponseType_AFPropertyListResponseSerializer:
            reponseSerializer= [AFPropertyListResponseSerializer serializer];
            break;
        default:
            break;
    }
    [manager setResponseSerializer:reponseSerializer];
    
}
+ (void)postWithURL:(NSString *)urlstring
       noSignParams:(NSDictionary *)params
    completionblock:(void (^)(NSDictionary *resultDict))completionBlock
        failedBlock:(void (^)(NSError *error))failedBlock {
    [self postWithURL:urlstring requestType:SYNetworkingRequestType_AFJSONRequestSerializer responseType:SYNetworkingResponseType_AFJSONResponseSerializer params:params completionblock:completionBlock failedBlock:failedBlock];
}
+ (void)postWithURL:(NSString *)urlstring
        requestType:(SYNetworkingRequestType)requestType
       responseType:(SYNetworkingReponseType)responseType
             params:(NSDictionary *)params
    completionblock:(void (^)(NSDictionary *resultDict))completionBlock
        failedBlock:(void (^)(NSError *error))failedBlock {
    RACSignal *signal  =[self rac_postURL:urlstring requestType:requestType responseType:responseType params:params];
    [signal subscribeNext:^(id  _Nullable x) {
        completionBlock(x);
        
    } error:^(NSError * _Nullable error) {
        failedBlock(error);
    }  ];
}
+(RACSignal *)rac_postURL:(NSString *)url
                   params:(NSDictionary*)params{
    return [self rac_postURL:url requestType:SYNetworkingRequestType_AFJSONRequestSerializer responseType:SYNetworkingResponseType_AFJSONResponseSerializer params:params];
}

+(RACSignal *)rac_postURL:(NSString *)url
                   params:(NSDictionary*)params retry:(NSInteger)tryCount interval:(NSTimeInterval)interval{
      return [self  rac_postURL:url requestType:SYNetworkingRequestType_AFJSONRequestSerializer responseType:SYNetworkingResponseType_AFJSONResponseSerializer params:params retry:tryCount interval:interval];
}

+(RACSignal *)rac_postURL:(NSString *)url
              requestType:(SYNetworkingRequestType)requestType
             responseType:(SYNetworkingReponseType)responseType
                   params:(NSDictionary*)params {
    return [self  rac_postURL:url requestType:requestType responseType:responseType params:params retry:-1 interval:0];
    
}
+(RACSignal *)rac_postURL:(NSString *)url
              requestType:(SYNetworkingRequestType)requestType
             responseType:(SYNetworkingReponseType)responseType
                   params:(NSDictionary*)params retry:(NSInteger)tryCount interval:(NSTimeInterval)interval  {
    RACSignal *signal= [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        SYNetworking*apiManager= [SYNetworking new];
        [apiManager createManger];
        [apiManager configRequest:requestType];
        [apiManager configReponse:responseType];
        
        NSLog(@"API Request -> %@", url);
        [apiManager->manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"API Reponse Success -> %@", responseObject);
            [subscriber sendNext:responseObject];
            [subscriber sendCompleted];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"API Reponse Error -> %@", error);
            [subscriber sendError:error];
        }];
        return [RACDisposable disposableWithBlock:^{
            [apiManager->manager invalidateSessionCancelingTasks:YES];
        }];
    }];
    if(tryCount<0){
        return signal;
    }
    return  [signal retry:tryCount interval:interval];
}
@end
